package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    //处理查询所有
    //shiro的权限表达式注解,可以以数组的形式配置多个权限,默认是and并存权限,可修改为or
    @RequiresPermissions(value = {"role:list","角色列表"},logical = Logical.OR)
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "role/list";//找视图
    }

    //去新增
    @RequestMapping("input")
    public String input(Model model,Long id){
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);

        if (id != null) {
            model.addAttribute("role",roleService.selectById(id));
        }
        return "/role/input";//找视图

    }
    @RequestMapping("delete")
    public String delete(Long id){
        if (id != null) {
            roleService.deleteById(id);
        }
        return "redirect:/role/list.do";//让浏览器重新发一次请求
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids ){
        if (role.getId() == null) {
            roleService.save(role,ids);
        }else {
            roleService.updateById(role,ids);
        }

        return "redirect:/role/list.do";//让浏览器重新发一次请求
    }
}
